package com.hema.auction.fragment;


import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.activity.GoodsDetailsActivity;
import com.hema.auction.adapter.NewsAdapter;
import com.hema.auction.base.BaseFragment;
import com.hema.auction.bean.GoodsInfo;
import com.hema.auction.event.NoticeDataEvent;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.utils.Utils;
import com.hema.auction.widget.view.EmptyView;
import com.hema.auction.widget.view.MyLoadMoreView;
import com.hema.auction.widget.view.RefreshRecyclerView;
import com.hema.auction.widget.view.TitleView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import ezy.ui.view.NoticeView;
import okhttp3.Call;


/**
 * author by john
 * 最新揭晓
 */
public class NewsFragment extends BaseFragment {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.notice_view)
    NoticeView noticeView;
    @BindView(R.id.refresh_recycler_view)
    RefreshRecyclerView refreshRecyclerView;
    private NewsAdapter newsAdapter;
    private List<GoodsInfo> goodsInfos = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.fragment_news;
    }

    @Override
    protected void init() {
        super.init();
        EventBus.getDefault().register(this);
        newsAdapter = new NewsAdapter(goodsInfos);
        newsAdapter.openLoadAnimation();
        newsAdapter.setEnableLoadMore(true);
        newsAdapter.setOnLoadMoreListener(this, refreshRecyclerView.getRecyclerView());
        EmptyView emptyView = new EmptyView(getActivity());
        emptyView.setViewType(EmptyView.OTHER);
        newsAdapter.setEmptyView(emptyView);
        newsAdapter.setLoadMoreView(new MyLoadMoreView());
        refreshRecyclerView.setVerLayoutManager();
        refreshRecyclerView.setOnRefreshListener(this);
        refreshRecyclerView.setRefresh(true);
        refreshRecyclerView.setAdapter(newsAdapter);
        refreshRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .size(1).color(getResources().getColor(R.color.colorDivider)).build());
        newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!Utils.isEmpty(newsAdapter.getData().get(position).getId())) {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(Constant.EXTRA_GOOD_INFO, newsAdapter.getData().get(position));
                    intentTo(GoodsDetailsActivity.class, bundle);
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        pageIndex = 1;
        HttpManager.getInstance(getActivity()).getNewestOrders(pageIndex, pageSize, this);
//        HttpManager.getInstance(getActivity()).getOrderBroadcast(NewsFragment.this);
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        pageIndex++;
        HttpManager.getInstance(getActivity()).getNewestOrders(pageIndex, pageSize, this);
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_NEWST_ORDERS:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<GoodsInfo> goodsInfos = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<GoodsInfo>>() {
                        }.getType());
                        if (pageIndex == 1) {
                            newsAdapter.setNewData(goodsInfos);
                            if (goodsInfos.size() < pageSize)
                                newsAdapter.setEnableLoadMore(false);
                            else
                                newsAdapter.setEnableLoadMore(true);

                        } else {
                            newsAdapter.addData(goodsInfos);
                            newsAdapter.loadMoreComplete();
                            if (goodsInfos.size() < pageSize)
                                newsAdapter.loadMoreEnd();
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onNoticeDataEvent(NoticeDataEvent event) {
        noticeView.start(event.getNotices());
    }

    @Override
    protected void onFinish(HttpTag httpTag) {
        super.onFinish(httpTag);
        refreshRecyclerView.setRefresh(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}

